package appnode;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.logging.Logger;
import javax.jdo.PersistenceManager;
import javax.servlet.http.*;
import javax.servlet.*;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import javax.jdo.Query;

import appnode.Node;
import appnode.Interface;
import appnode.SearchNode;
import appnode.PMF;

public class EditLink extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException  {
	
		PersistenceManager pm = PMF.get().getPersistenceManager();

		String keystr = req.getParameter("keystr");
		String freq = req.getParameter("freq");
		String protocol = req.getParameter("protocol");
		String power = req.getParameter("power");
		String delete = req.getParameter("delete");
		
		Key key = KeyFactory.stringToKey(keystr);
		Link link = pm.getObjectById(Link.class,key);
		
		
		if (delete.equals("")) {
			link.setFreq(freq);
			link.setProtocol(protocol);
			link.setPower(power);
			resp.setContentType("text/html");
			resp.getWriter().print("<html>");
			resp.getWriter().print("<head><title>Edit your link</title></head>");
			resp.getWriter().print("<div style='font-family:Verdana;font-size: smaller;'>You change your link succesfully!</div>");
			resp.getWriter().print("<br>");
			resp.getWriter().print("<input type='button' value='Ok' onClick='self.close()'/>");
			resp.getWriter().print("</html>");
			
		} // delete
		else {
			
			Key keyif1 = KeyFactory.stringToKey(link.getName1());
			Interface if1 = pm.getObjectById(Interface.class,keyif1);
			
			Key keyif2 = KeyFactory.stringToKey(link.getName2());
			Interface if2 = pm.getObjectById(Interface.class,keyif2);
			
			if1.setLink("");
			if1.setFrominterface("");
			if2.setLink("");
			if2.setFrominterface("");
			
			pm.deletePersistent(link);
			
			resp.setContentType("text/html");
			resp.getWriter().print("<html>");
			resp.getWriter().print("<div style='font-family:Verdana;font-size: smaller;'>You deleted your link!</div>");
			resp.getWriter().print("<input type='button' value='Ok' onClick='self.close()'/>");
			resp.getWriter().print("</html>");
		} 
	
	pm.close();
	}
}